package com.bzyd.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bzyd.mp.model.qo.UserQO;
import com.bzyd.mp.model.po.User;
import com.bzyd.mp.mapper.UserMapper;
import com.bzyd.mp.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xianyue Dang
 * @since 2021-08-24
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public IPage<User> pageQuery1(UserQO userQO) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(userQO.getName()), User::getName, userQO.getName());
        wrapper.eq(StringUtils.isNotBlank(userQO.getMobile()), User::getMobile, userQO.getMobile());
        wrapper.ge(Objects.nonNull(userQO.getAge()), User::getAge, userQO.getAge());
        if (Objects.nonNull(userQO.getCreateDateEnd())) {
            //日期往后加一天
            userQO.setCreateDateEnd(userQO.getCreateDateEnd().plusDays(1));
        }
        wrapper.between(Objects.nonNull(userQO.getCreateDateStart()) && Objects.nonNull(userQO.getCreateDateEnd()),
                User::getCreateTime, userQO.getCreateDateStart(), userQO.getCreateDateEnd());
        wrapper.orderByDesc(User::getCreateTime);
        Page<User> page = new Page<>(userQO.getCurrentPage(), userQO.getPageSize());
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public IPage<User> pageQuery2(UserQO userQO) {
        if (Objects.nonNull(userQO.getCreateDateEnd())) {
            //日期往后加一天
            userQO.setCreateDateEnd(userQO.getCreateDateEnd().plusDays(1));
        }
        Page<User> page = new Page<>(userQO.getCurrentPage(), userQO.getPageSize());
        List<User> list = baseMapper.findList(page, userQO);
        return page.setRecords(list);
    }
}
